package edu.hgnc.tx.xml.dao;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.aop.framework.AopContext;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;
import java.util.Map;

// @Repository
public class BookDao {

	private static final Log logger = LogFactory.getLog(BookDao.class);

	// @Autowired
	private JdbcTemplate jdbcTemplate;

	// @Autowired
	// private BookDao bookDao;

	public void test(){
		logger.debug("bookDao -----------------------");
		List<Map<String, Object>> mapList = jdbcTemplate.queryForList("select * from test_db.table_name");
		logger.debug("=========================================================");
		for (Map<String, Object> e : mapList) {
			logger.debug(e);
		}
		logger.debug("=========================================================");
	}

	/**
	 * 更新方法
	 * @param id
	 */
	public void updateStock(Integer id){
		logger.debug("bookDao --updateStock---------------------");
		String sql = "update book_stock set stock = stock -1 where id = ?";
		int i = jdbcTemplate.update(sql, id);

		for (int a = 4; a >=0; a--) {
			// System.out.println(10/a);
		}

		logger.debug("=========================================================");
		logger.debug("update result:"+i);

		logger.debug("=========================================================");
	}
	public void updateStock2(Integer id){

		BookDao dao = (BookDao) AopContext.currentProxy();
		dao.updateStock(2);



		logger.debug("bookDao --updateStock---------------------");

		logger.debug("=========================================================");
	}

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		logger.debug("BookDao--使用了set 注入--");
		this.jdbcTemplate = jdbcTemplate;
	}
}
